package server.business.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import server.business.pojo.TransfusionApplicationInformation;
import server.business.service.TransfusionApplicationInformationService;
import server.business.mapper.TransfusionApplicationInformationMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Galaxy
* @description 针对表【transfusion_application_information】的数据库操作Service实现
* @createDate 2022-11-08 12:46:24
*/
@Service("transfusionApplicationInformationService")
public class TransfusionApplicationInformationServiceImpl extends ServiceImpl<TransfusionApplicationInformationMapper, TransfusionApplicationInformation>
    implements TransfusionApplicationInformationService{




    // *************************************lrz*************************start

    @Autowired
    private TransfusionApplicationInformationMapper transfusionApplicationInformationMapper;

    /**
     *
     * @param currentPage
     * @param pageSize
     * @param
     * @return
     */
    @Override
    @SuppressWarnings("")
    public IPage<TransfusionApplicationInformation> getPage(int currentPage, int pageSize,
                                                            TransfusionApplicationInformation transfusionApplicationInformation) {
        LambdaQueryWrapper<TransfusionApplicationInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Strings.isNotBlank(transfusionApplicationInformation.getTransfusionNumber()),
                        TransfusionApplicationInformation::getTransfusionNumber,
                        transfusionApplicationInformation.getTransfusionNumber())
                .eq(Strings.isNotBlank(transfusionApplicationInformation.getAboBloodType()),
                        TransfusionApplicationInformation::getAboBloodType,
                        transfusionApplicationInformation.getAboBloodType())
                .eq(Strings.isNotBlank(transfusionApplicationInformation.getRhBloodType()),
                        TransfusionApplicationInformation::getRhBloodType,
                        transfusionApplicationInformation.getRhBloodType())
                .eq(Strings.isNotBlank(transfusionApplicationInformation.getBloodComponent()),
                        TransfusionApplicationInformation::getBloodComponent,
                        transfusionApplicationInformation.getBloodComponent())
                .eq(transfusionApplicationInformation.getBloodCount() != null,
                        TransfusionApplicationInformation::getBloodCount,
                        transfusionApplicationInformation.getBloodCount())
                .eq(Strings.isNotBlank(transfusionApplicationInformation.getPatientId()),
                        TransfusionApplicationInformation::getPatientId,
                        transfusionApplicationInformation.getPatientId())
                .eq(Strings.isNotBlank(transfusionApplicationInformation.getBloodNumber()),
                        TransfusionApplicationInformation::getBloodNumber,
                        transfusionApplicationInformation.getBloodNumber())
                .eq(Strings.isNotBlank(transfusionApplicationInformation.getState()),
                        TransfusionApplicationInformation::getState,
                        transfusionApplicationInformation.getState());
        IPage<TransfusionApplicationInformation> page = new Page<>(currentPage, pageSize);
        transfusionApplicationInformationMapper.selectPage(page, queryWrapper);
        return page;
    }

    /**
     * 返回所有配血信息
     *
     * @return
     */
    @Override
    public List<TransfusionApplicationInformation> getAll() {
        return this.list();
    }

    /**
     * 添加配血记录
     *
     * @param
     */
    @Override
    public void addRecord(TransfusionApplicationInformation transfusionApplicationInformation) {
        transfusionApplicationInformationMapper.insert(transfusionApplicationInformation);
    }

    /**
     * 根据血液编号更改血液分配表状态
     *
     * @param bloodNumber
     */
    @Override
    public void setStateByBloodNumber(String bloodNumber, String state) {
        LambdaUpdateWrapper<TransfusionApplicationInformation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Strings.isNotBlank(bloodNumber), TransfusionApplicationInformation::getBloodNumber,
                bloodNumber);
        updateWrapper.set(TransfusionApplicationInformation::getState, state);
        transfusionApplicationInformationMapper.update(null, updateWrapper);
    }

    /**
     * 检测血液分配表中是否已经有了记录
     */
    @Override
    public List<TransfusionApplicationInformation> checkRecords(String patientId, String transfusionNumber) {
        LambdaQueryWrapper<TransfusionApplicationInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TransfusionApplicationInformation::getPatientId, patientId)
                .eq(TransfusionApplicationInformation::getTransfusionNumber, transfusionNumber);
        List<TransfusionApplicationInformation> list = transfusionApplicationInformationMapper
                .selectList((queryWrapper));
        if (list == null) {
            return null;
        }
        return list;
    }

    // *************************************lrz*************************end

}




